--[[----------------------------------------------------------------------------
LR2RotationShadowSection.lua
--------------------------------------------------------------------------------
Copyright 2010 Andriy Doroshchuk
 All Rights Reserved.
------------------------------------------------------------------------------]]

require 'LR2Util_Logger'
require 'LR2Util_IM'

-- Lightroom SDK
local LrView 		= import 'LrView'
local LrColor 		= import 'LrColor'

--============================================================================--
local LR2RotationShadowSection = {}
-------------------------------------------------------------------------------
LR2RotationShadowSection.exportPresetFields = {
	{ key = 'rotationValue',	default = 0					},
	{ key = 'rotationRandom',	default = false				},
	{ key = 'backgroundColor',	default = LrColor('white')	},
}
-------------------------------------------------------------------------------
function LR2RotationShadowSection.sectionForFilterInDialog( viewFactory, propertyTable )
	local vf = viewFactory
	local rotationBinding = LrView.bind {
		key 		= 'rotationRandom', 
		object		= propertyTable,
		transform	= function (value, fromTable) return not value end,
	}
	
	return {
		title 		= LOC '$$$/LR2/PhotoFrames/RotationShadow/SectionTitle=LR2/PhotoFrames Rotation and Shadow', 
		synopsis	= LrView.bind {
			keys 		= {'rotationValue', 'rotationRandom'},
			object 		= propertyTable,
			operation 	= function (binder, values, fromTable) 
				if values.rotationRandom then
					return LOC '$$$/LR2/PhotoFrames/RotationShadow/RandomRotation=Random Rotation'
				else
					return (LOC '$$$/LR2/PhotoFrames/RotationShadow/Rotation=Rotation') .. ':' .. values.rotationValue
				end
			end,
		},
		
		vf:row {
			spacing = vf:control_spacing(),
			vf:static_text {
				title 		= LOC '$$$/LR2/PhotoFrames/RotationShadow/Rotation=Rotation',
				alignment 	= 'right',
				width		= 100,
			},
			vf:edit_field {
				width_in_digits = 3,
				min 		= -45,
				max 		= 45,
				precision 	= 0,
				value 		= LrView.bind('rotationValue'),
				enabled		= rotationBinding,
			},
			vf:slider {
				min 		= -45,
				max 		= 45,
				integral 	= true,
				width 		= 200,
				value 		= LrView.bind('rotationValue'),
				enabled		= rotationBinding,
			},
			vf:checkbox {
				title 		= LOC '$$$/LR2/PhotoFrames/RotationShadow/RandomRotation=Random Rotation',
				value 		= LrView.bind('rotationRandom'),
			},
		},
		
		vf:row {
			vf:static_text {
				title 		= LOC '$$$/LR2/PhotoFrames/RotationShadow/BackgroundColor=Background Color',
				alignment 	= 'right',
				width		= 100,
			},
			vf:color_well {
				value 		= LrView.bind('backgroundColor'),
			},
		},
	}
	
end
-------------------------------------------------------------------------------
function LR2RotationShadowSection.postProcessRenderedPhotos( functionContext, filterContext )
	local renditionOptions = {
		plugin 				= _PLUGIN,
		renditionsToSatisfy = filterContext.renditionsToSatisfy,
		filterSettings 		= function( renditionToSatisfy, exportSettings )
		end,
	}

	local bcolor	= makeColor(filterContext.propertyTable.backgroundColor)
	local rotation 	= filterContext.propertyTable.rotationValue
	if filterContext.propertyTable.rotationRandom then
		rotation 	= math.random(-45, 45)
	end
	
	local command	= '-background none -rotate ' .. rotation .. ' -background black ' .. lbracket .. ' +clone -shadow 60x4+4+4 ' .. rbracket .. ' +swap ' ..
			'-background ' .. bcolor .. ' -flatten '
	
	local toolPath = GetTool( filterContext.propertyTable.pathToIM )
	
	for sourceRendition, renditionToSatisfy in filterContext:renditions(renditionOptions) do
		local success, pathOrMessage = sourceRendition:waitForRender()
		if success and toolPath then
			RunIM(toolPath, sourceRendition.destinationPath, command, renditionToSatisfy)
		end
	end
	
end
--------------------------------------------------------------------------------

return LR2RotationShadowSection

